<?php
/*
 * Created on 28-sept.-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class SnapshotsController extends Zend_Controller_Action
 {
    public function indexAction()
    {
    	$db = Zend::registry('db');
        
        $get = Zend::registry('fGet');        
        $myThreadId = $get->getRaw('threadId');        
        $mySelectedDate = $get->getRaw('selectedDate');
        $mySelectedSnapshot = $get->getRaw('selectedSnapshot');

        $sql = "SELECT distinct(date) FROM security WHERE camera = '" . $myThreadId . "' AND file_type = '2' ";
        if ( $mySelectedDate > "" )
        {
            $sql .= "AND date = '" . $mySelectedDate . "' ";
        }
        $sql .= "ORDER BY date DESC";
        $myDates = $db->fetchAll($sql);

        $SnapshotsParDate = array();
        foreach ( $myDates as $myDate )
        {
            $mySnapshotsDate = $myDate['date'];
            
            $sql = "SELECT * FROM security, threads WHERE camera = id AND file_type = '2' " .
                    "AND camera = '" . $myThreadId . "' AND date = '" . $mySnapshotsDate . "' ORDER BY time_stamp DESC";
            $mySnapshotsSelection = $db->fetchAll($sql);  
               
            $mySnapshots = array();
            foreach ($mySnapshotsSelection as $mySnapshot)
            {                                                                                               
                if ( $mySelectedSnapshot > "" )
                {
                    if ( ereg( $mySelectedSnapshot, $mySnapshot['filename'] ) ) $mySnapshotClass = "selected";
                        else $mySnapshotClass = "";	
                } else $mySnapshotClass = "";
                
                $mySnapshotPath = C_MOTION_DIR . $mySnapshot['name'] . '/' . basename($mySnapshot['filename']);
                $myMiniPath = photo_mini(C_MOTION_DIR . $mySnapshot['name'] . '/' . basename($mySnapshot['filename']));
                $mySnapshotHeure = $mySnapshot['heure'];                 
                array_push ($mySnapshots, array("path"=>$mySnapshotPath, "mini"=>$myMiniPath,  "heure"=>$mySnapshotHeure, "class"=>$mySnapshotClass));    
            }
            array_push($SnapshotsParDate, array("date"=>$mySnapshotsDate, "data"=>$mySnapshots));
        }   

        $listeSnap = Zend::registry('view');
        $listeSnap->threadId = $myThreadId;
        $listeSnap->selectedDate = $mySelectedDate;
        $listeSnap->selectedSnapshot = $mySelectedSnapshot;
        $listeSnap->snapshotsParDate = $SnapshotsParDate;   
        $listeSnap->visu = $this->consulterAction();       
        echo $listeSnap->render('snapshotsTableauView.php');   
    }
    
    public function consulterAction()
    {
        $db = Zend::registry('db');
        
        $get = Zend::registry('fGet');        
        $myThreadId = $get->getRaw('threadId');
        $mySelectedSnapshot = $get->getRaw('selectedSnapshot');        
        $mySelectedDate = $get->getRaw('selectedDate');
        
        if ( $mySelectedSnapshot > "" )
        {
            // Definition des donnees du Snapshot en cour, precedent et suivant //
            $sql = "SELECT * FROM security, threads WHERE camera = id AND file_type = '2' " .
                    "AND camera = '" . $myThreadId . "' ";
            if ( $mySelectedDate > "" ) $sql .= "AND date = '" . $mySelectedDate . "' ";
            $sql .= "ORDER BY time_stamp DESC";
            $mySnapshotsSelection = $db->fetchAll($sql);
            
            if ( count($mySnapshotsSelection) > 0 )
            {
                for ($countSnap=0; $countSnap < count($mySnapshotsSelection); $countSnap++)
                {           
                    $mySnapshot = $mySnapshotsSelection[$countSnap];                       
                    if ( ereg( $mySelectedSnapshot, $mySnapshot['filename'] ) )
                    {
                        $mySelectedSnapshotTitre = $mySnapshot['date'] . " : " . $mySnapshot['heure'];                    
                                        
                        if ( $countSnap > 0 )$myPreviousSnapshot = C_MOTION_DIR . $mySnapshot['name'] . '/' . basename($mySnapshotsSelection[$countSnap -1]['filename']);
                            else $myPreviousSnapshot = $mySelectedSnapshot;
                        if ( $countSnap +1 < count($mySnapshotsSelection) ) $myNextSnapshot = C_MOTION_DIR . $mySnapshot['name'] . '/' . basename($mySnapshotsSelection[$countSnap +1]['filename']);
                            else $myNextSnapshot = $mySelectedSnapshot;                               
                    }
                }
                                        
            }        	

            $view = Zend::registry('view');
            $view->threadId = $myThreadId;
            $view->selectedSnapshot = $mySelectedSnapshot;         
            $view->selectedSnapshotTitre = $mySelectedSnapshotTitre;
                       
            $view->previousSnapshot = $myPreviousSnapshot;
            $view->nextSnapshot = $myNextSnapshot;     
            
            $view->selectedDate = $mySelectedDate;    
            return $view->render('snapshotsConsultView.php');
        }             
    }
    
    public function deleteAction()
    {
        $db = Zend::registry('db');
        $get = Zend::registry('fGet');         
        $myThreadId = $get->getRaw('threadId');             
        $mySelectedSnapshot = $get->getRaw('selectedSnapshot');
        $mySelectedDate = $get->getRaw('selectedDate');    
        
        if ( file_exists( $mySelectedSnapshot ) ){        
            $myMiniFile = C_MOTION_DIR."photo_".md5(strstr($mySelectedSnapshot, C_MOTION_DIR)).".png";
            if ( file_exists( $myMiniFile ) ) unlink($myMiniFile);

            unlink( $mySelectedSnapshot );
            $sql = "DELETE FROM security WHERE file_type = '2' AND filename LIKE '%" . $mySelectedSnapshot . "'";
            $db->query($sql);
        }

        $this->_redirect('/snapshots?threadId=' . $myThreadId . '&selectedDate=' . $mySelectedDate); 
    }
    public function selectionAction()
    {
        $db = Zend::registry('db');
        $post = Zend::registry('fPost');        
        $myThreadId = $post->getRaw('threadId');
        $myFileCount = $post->getRaw('fileCount');
        $mySelectedDate = $post->getRaw('selectedDate'); 

        for ($i = 0; $i < $myFileCount; $i++){
            $myFile = $post->getRaw('checkbox'.$i);
        
            if ( $myFile > "" ){
                // SUPPRESSION DES FICHIER ET DES ENTREES EN BASE DE DONNEES                        
                if ( file_exists( $myFile )) {
                    $myMiniFile = C_MOTION_DIR."photo_".md5(strstr($myFile, C_MOTION_DIR)).".png";
                    if ( file_exists($myMiniFile) ) unlink( $myMiniFile );
        
                    unlink( $myFile );                
                }
                
        
                $sql = "DELETE FROM security WHERE file_type = '2' AND filename LIKE '%" . $myFile . "'";
                $db->query($sql); 	
            }   
        }

        $sql = "SELECT count(filename) FROM security WHERE file_type = '2' AND camera = '" . $myThreadId . "'";
        $myCountFile = $db->fetchOne($sql);
        
        if ( $myCountFile > 0 ) $this->_redirect('/snapshots?threadId=' . $myThreadId . '&selectedDate=' .$mySelectedDate);
            else $this->_redirect('/camera?threadId=' . $myThreadId);        
    }

    public function noRouteAction()
    {
        $this->_redirect('/');
    }
}    
?>
